package com.hanfengyi.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.hanfengyi.feign.RedisFeignClient;
import com.hanfengyi.hrm.AjaxResult;
import com.hanfengyi.hrm.MD5;
import com.hanfengyi.user.domain.Sso;
import com.hanfengyi.user.domain.VipBase;
import com.hanfengyi.user.dto.RegistDTO;
import com.hanfengyi.user.dto.SendEMSDTO;
import com.hanfengyi.user.mapper.SsoMapper;
import com.hanfengyi.user.mapper.VipBaseMapper;
import com.hanfengyi.user.service.ISsoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author hfy
 * @since 2020-03-02
 */
@Service
public class SsoServiceImpl extends ServiceImpl<SsoMapper, Sso> implements ISsoService {

    @Autowired
    private VipBaseMapper vipBaseMapper;
    @Autowired
    private RedisFeignClient redisFeignClient;

    @Override
    public void register(RegistDTO registDTO) {
        //1. 参数校验
        String phone = registDTO.getMobile();
        String password = registDTO.getPassword();
        if(!StringUtils.hasLength(phone)
                || !StringUtils.hasLength(password)
                || !StringUtils.hasLength(registDTO.getImageCode())
                || !StringUtils.hasLength(registDTO.getSmsCode()))
            throw new RuntimeException("无效的参数！");

        //2.判断用户是否是接收手机验证码的用户
        String mobileAsKey = "sms:"+phone;
        AjaxResult ajaxResult = redisFeignClient.get(mobileAsKey);
        if(!ajaxResult.isSuccess() || ajaxResult.getResultObj()==null)
            throw new RuntimeException("手机号码无效！");

        //判断用户输入的手机验证码是否正确
        SendEMSDTO sendEMSDTO = JSON.parseObject(ajaxResult.getResultObj().toString(), SendEMSDTO.class);
        if(!sendEMSDTO.getSendCode().equals(registDTO.getSmsCode()))
            throw new RuntimeException("手机验证码错误！");

        //2. 判读用户是否存在
        Sso registDTOInDB = baseMapper.selectByPhone(phone);
        if(registDTOInDB!=null)
            throw new RuntimeException("用户已存在！");

        //3. 对密码进行加密(使用随机盐值)，存储到数据库中
        long time = new Date().getTime();

        String salt = MD5.getRandomCode(16);
        String pwdUseMd5 = MD5.getMD5(registDTO.getPassword() + salt);
        Sso newSso = new Sso();
        newSso.setPhone(registDTO.getMobile());
        newSso.setPassword(pwdUseMd5);
        newSso.setCreateTime(time);
        newSso.setSalt(salt);
        baseMapper.insert(newSso);

        //4. 同步更新t_vip_base表
        VipBase vipBase = new VipBase();
        vipBase.setRegChannel(1);
        vipBase.setRegTime(time);
        vipBase.setSsoId(newSso.getId());
        vipBase.setCreateTime(time);
        vipBaseMapper.insert(vipBase);
    }

    @Override
    public String login(Sso sso) {
        //1. 参数校验
        if(!StringUtils.hasLength(sso.getPhone()))
            throw new RuntimeException("手机号不能为空");
        if(!StringUtils.hasLength(sso.getPassword()))
            throw new RuntimeException("密码不能为空！");

        //2. 判断手机号是否存在
        Sso ssoFromDB = baseMapper.selectByPhone(sso.getPhone());
        if(ssoFromDB==null)
            throw new RuntimeException("用户不存在！");

        //3. 密码校验
       String pwdByMD5 = MD5.getMD5(sso.getPassword()+ssoFromDB.getSalt());
       if(!pwdByMD5.equals(ssoFromDB.getPassword()))
           throw new RuntimeException("手机或者密码错误!");

       //4. 登录成功后生成随机码作为token,存储到redis中,并返回
        String token = UUID.randomUUID().toString();
        redisFeignClient.setex(token, 60*60*24*7, JSON.toJSONString(ssoFromDB));
        return token;
    }
}
